package com.ruoyi.blindBoxManage.service.impl;

import com.ruoyi.blindBoxManage.domain.PlayType;
import com.ruoyi.blindBoxManage.mapper.BlindBoxMapper;
import com.ruoyi.blindBoxManage.mapper.PlayTypeMapper;
import com.ruoyi.blindBoxManage.service.IPlayTypeService;
import com.ruoyi.common.utils.DateUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 玩法类型Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@Service
public class PlayTypeServiceImpl implements IPlayTypeService {
    @Autowired
    private PlayTypeMapper playTypeMapper;
    @Resource
    private BlindBoxMapper blindBoxMapper;

    /**
     * 查询玩法类型
     *
     * @param id 玩法类型主键
     * @return 玩法类型
     */
    @Override
    public PlayType selectPlayTypeById(Integer id) {
        return playTypeMapper.selectPlayTypeById(id);
    }

    /**
     * 查询玩法类型列表
     *
     * @param playType 玩法类型
     * @return 玩法类型
     */
    @Override
    public List<PlayType> selectPlayTypeList(PlayType playType) {
        return playTypeMapper.selectPlayTypeList(playType);
    }

    /**
     * 新增玩法类型
     *
     * @param playType 玩法类型
     * @return 结果
     */
    @Override
    public int insertPlayType(PlayType playType) {
        playType.setCreateTime(DateUtils.getNowDate());
        return playTypeMapper.insertPlayType(playType);
    }

    /**
     * 修改玩法类型
     *
     * @param playType 玩法类型
     * @return 结果
     */
    @Override
    public int updatePlayType(PlayType playType) {
        playType.setUpdateTime(DateUtils.getNowDate());
        return playTypeMapper.updatePlayType(playType);
    }

    /**
     * 批量删除玩法类型
     *
     * @param ids 需要删除的玩法类型主键
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int deletePlayTypeByIds(Integer[] ids) {
        for (Integer id : ids) {
            //查询盲盒是否有绑定的玩法
            Integer count = blindBoxMapper.selectCountByPlayTypeId(id);
            if (count > 0) {
                throw new RuntimeException("id为:" + id + " 有盲盒绑定");
            }
        }
        return playTypeMapper.deletePlayTypeByIds(ids);
    }

    /**
     * 删除玩法类型信息
     *
     * @param id 玩法类型主键
     * @return 结果
     */
    @Override
    public int deletePlayTypeById(Integer id) {
        return playTypeMapper.deletePlayTypeById(id);
    }
}
